Skip to content

Conversation

@gthb
Copy link

@gthb gthb commented Mar 23, 2025

What

Parse the string values inside "scripts" in a JSON file (targeting package.json files) as language Bash.

Why

To get a more minimal diff of those script values when only a small part of the shell script is changed

Todo

  • Get it working that the injected language subtree gets minimally diffed (not clear to me why it isn't, see Status below).
  • Target more specifically to only act where appropriate, based on some of:
    • filename being package.json or some pattern closely matching that
    • the presence of name and version and other telltale top-level properties characterizing a package.json file
    • explicit user choice (cluttering up the CLI and environment-variable namespaces, maybe not great...)

Status

The sublanguage detection does work, as confirmed when I insert dbg! statements into parse_subtrees: the intended nodes are recognized and recorded in the subtrees hashmap that's returned.

But that's not having the intended effect and I'm not sure why. The changed subparts of the script string do get highlighted individually, but the whole script string is red in the original and green in the current file:
image

What I was after was the same behavior I see with the existing sub-language setup for Css in the style element of an Html tree, where only the changed parts of the subtree are colored red and green:
image

The Bash language does do that minimal diffing, when I extract those shell strings to files and difft them directly:
image

So why don't I get that behavior when these shell strings are diffed in sub-language subtrees?

Example files

package-orig.json:

{
  "name": "example",
  "version": "0.1.0",
  "scripts": {
    "build": "wp",
    "doctest:profile": "mkdir -p ${OUTPUT_FOLDER:-output} ; mkdir -p .env ; NODE_OPTIONS=--enable-source-maps chpst -e .env node --cpu-prof --cpu-prof-dir ${OUTPUT_FOLDER:-output} --loader ts-node/esm/transpile-only --experimental-wasm-modules --expose-gc --max_old_space_size=30000 src/doctest.ts",
    "typecheck": "tsc --noEmit"
  }
}

package-changed.json:

{
  "name": "example",
  "version": "0.1.0",
  "scripts": {
    "build": "wp",
    "doctest:profile": "mkdir -p ${OUTPUT_FOLDER:-output} ; mkdir -p .env ; NODE_OPTIONS=--enable-source-maps chpst -e .env npx pprof-it --loader ts-node/esm/transpile-only --experimental-wasm-modules --max_old_space_size=30000 src/doctest.ts",
    "typecheck": "tsc --noEmit"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant